package com.lcc.utils;

import org.apache.ibatis.io.Resources;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;

import java.io.IOException;
import java.io.InputStream;

/**
 * @author lincc
 * @date 1.0 2021/9/16
 */
public class MyBatisUtil {
    private static SqlSessionFactory factory;

    //初始化SqlSessionFactory
    static {
        try (InputStream inputStream = Resources.getResourceAsStream("mybatis-config.xml")){
            SqlSessionFactoryBuilder builder = new SqlSessionFactoryBuilder();
            factory = builder.build(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
            System.exit(0);
        }
    }

    /**
     * 获取sqlSession
     */
    public static SqlSession getSession(){
        return factory.openSession();
    }
}
